package com.qdu.dao;

import com.qdu.entity.Movie;
import com.qdu.entity.Worker;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Dao类-数据访问对象类，专门用于访问数据库中的表
 * <br>MovieDao包含方法用于对Movie表执行增删改查， 这里只封装了一个方法用于查询
 *
 * @author Anna
 */
public class MovieDao {

    /**
     * 根据电影编号查询电影信息
     *
     * @param movieId 字符串表示的电影编号
     * @return 一个Movie对象，包含一个电影的所有信息
     */
    public Movie getMovieInfoById(String movieId) {
        
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from Movie where MovieId=?");
            ps.setString(1, movieId);
            rs = ps.executeQuery();
            while (rs.next()) {
                return new Movie(
                        rs.getString(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getFloat(4),
                        rs.getString(5),
                        rs.getString(6),
                        rs.getString(7),
                        rs.getString(8),
                        rs.getInt(9),
                        rs.getString(10),
                        rs.getString(11),
                        null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return null;
    }

    /**
     * 获取所有电影的基本信息，包括电影编号、名称、评分和图片
     *
     * @return 一个Movie列表，包含所有电影基本信息
     */
    public List<Movie> getAllMovies() {
        
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Movie> list = new ArrayList<>();
        
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select MovieId,MChineseName,MRating,MCover from Movie");
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Movie(rs.getString(1), rs.getString(2), rs.getFloat(3), rs.getString(4)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }

    /**
     * 根据电影名称关键字检索电影列表
     *
     * @param movieName 字符串表示的电影名称关键字
     * @return 一个Movie列表，包含查到的电影的相关信息
     */
    public List<Movie> getMoviesByKeyword(String movieName) {
        
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Movie> list = new ArrayList<>();
        
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select MovieId,MChineseName,MRating,MDigest,MCover from Movie where MChineseName like ?");
            ps.setString(1, "%" + movieName + "%");
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Movie(rs.getString(1), rs.getString(2), rs.getFloat(3), rs.getString(4), rs.getString(5)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }
}
